/**
 * 模块配置操作
 */
const path = require("path")
const { readJson, writeFile } = require('../util/fileUtil')

// 配置文件路径
const settingFile = path.resolve(process.cwd(), "datas", "setting.json")

/**
 * 获取本地配置项
 * @param {*} ctx 
 * @return 
 */
const onGetSetting = async (ctx) => {
	const { query } = ctx.request
	let settingJson = readJson(settingFile)
	if(!settingJson){
		settingJson = {}
	}
	ctx.send({ data: { code: 200, data:settingJson } })
}

/**
 * 保存配置项
 * @param {*} ctx 
 */
const onSaveSetting = async (ctx) => {
	const { body } = ctx.request
	try{
		let settingJson = readJson(settingFile)
		if(!settingJson){
			settingJson = {}
		}
		settingJson = {...settingJson, ...body}
		writeFile(settingFile, JSON.stringify(settingJson, null, 4))
		ctx.send({ data: { code: 200, msg: `配置更新成功` } })
	}
	catch(e){
		ctx.send({ data: { code: 300, msg: e.message } })
	}
}


module.exports = {
	onGetSetting,
	onSaveSetting
}